﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using EHR18.Models;
using System.Linq;
using System.Text.RegularExpressions;
using EHR18.Models;
using EHR18;

namespace Common
{
    public class CCDParser : DataPaser, IDataPaser
    {
        static List<string> colNames;
        public CCDParser(string fileName)
            : base(fileName)
        {
        }
        public List<string> getCorlumnNames()
        {
            List<string> slist = new List<string>();
            using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
            {
                string line = sr.ReadLine().TrimEnd(new char[] { ',' });
                if (string.IsNullOrEmpty(line))
                    throw new Exception("没有读到标题数据");
                string[] titles = line.Split(new char[] { ',' });
                foreach (string title in titles)
                {
                    slist.Add(title);
                }
            }
            return slist;
        }
        public List<string> getLastRecord()
        {
            ParseColNameAndMapToKV();
            List<string> slist = new List<string>();
            string line = ReadLastLine(FileName, 1, Encoding.Default).TrimEnd(new char[] { '\r', '\n', ',' });
            string[] fileds = line.Split(new char[] { ',' });
            foreach (string filed in fileds)
            {
                slist.Add(filed);
            }
            return slist;
        }
        /// <summary>
        /// 读取列名到数据表别名映射
        /// </summary>
        private void ParseColNameAndMapToKV()
        {
            if (colNames == null || colNames.Count == 0)
            {
                colNames = getCorlumnNames();
                Regex Rgx = new Regex(@"^A\d{1,2}$");
                var CamsNames = GenneralConfig.instance.module.NameMappings.Where(t => Rgx.IsMatch(t.Value)).ToArray();
                for (int i = 0; i < colNames.Count && i < CamsNames.Count(); i++)
                {
                    CamsNames[i].Name = colNames[i];
                }
                GenneralConfig.instance.SaveModuleConfig();
            }
        }
        public bool getSpans(ref List<Section> list)
        {
            try
            {
                if (colNames == null || colNames.Count == 0)
                {
                    colNames = getCorlumnNames();
                }
                var values = getLastRecord();
                string strResult = values[values.Count - 1];
                values.RemoveAt(values.Count - 1);
                bool bret;
              //  System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\d+(\.(\d)+)");
                double a = 0;
                if (double.TryParse(strResult,out a))
                {
                    double result = double.Parse(strResult);
                    bret = result > 0 ? true : false;
                }
                else
                {
                    bret = strResult.ToUpper().Contains("PASS");
                }
                for (int i = 0; i < values.Count; i++)
                {
                    list.Add(new Section
                    {
                        Station = "图像",
                        Item = colNames[i],
                        SubInfo = "Value",
                        Value = values[i]
                    });
                }
                list.Add(
                    new Section
                    {
                        Station = "图像",
                        Item = colNames[colNames.Count-1],
                        SubInfo = "Result",
                        Value = bret ? "PASS" : "FAIL"
                    });
                return bret;
            }
            catch (Exception EX)
            {
                return false;
            }
        }

        public bool getResult(ref List<TestSpan> tss, ref List<string> values)
        {
            values = getLastRecord();
            string strResult = values[values.Count - 1];
            values.RemoveAt(values.Count - 1);
            values.RemoveRange(0, 3);
            bool bret;
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\d+(\.(\d)+)");
            if (reg.IsMatch(strResult))
            {
                double result = double.Parse(strResult);
                bret = result > 0 ? true : false;
            }
            else
            {
                bret = strResult.ToUpper().Contains("PASS");
            }

            TestSpan ts = new TestSpan();
            if (!bret)
            {
                CountManager.AddFail("图像");
            }
            else
            {
                CountManager.AddPass("图像");
            }
            ts.测试结果 = (bret ? "PASS" : "FAIL");
            ts.测试项目 = "图像";
            ts.测试值 = (bret ? "PASS" : "FAIL");
            ts.上限值 = "NA";
            ts.下限值 = "NA";
            ts.不良数量 = CountManager.getCount("图像").NG.ToString();
            ts.不良率 = CountManager.getCount("图像").NgRate;
            tss.Add(ts);
            return bret;
        }
    }

}
